﻿
Imports DAL
Imports System.Linq
Imports System.Drawing.Drawing2D

Public Class UcTelephone

    Private ListeTel As List(Of DAL.Telephone)
    Event modification As EventHandler
    Private rowCtr As Integer = 0

    Private _lecture As Boolean = False
    Public Property Lecture() As Boolean
        Get
            Return _lecture
        End Get
        Set(ByVal value As Boolean)
            _lecture = value
            UpdateTableTel()
        End Set
    End Property

    Public Sub SetTelephones(ByVal liste As List(Of DAL.Telephone))
        ListeTel = liste
        UpdateTableTel()
    End Sub

    Public Function GetTelephones() As List(Of DAL.Telephone)
        Return ListeTel.Where(Function(x) x.Telephone <> String.Empty).ToList
    End Function

    'Mise en place d'un champ de téléphone
    Private Sub Telephone_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ListeTel = New List(Of Telephone)
        UpdateTableTel()
    End Sub

    Private Sub UpdateTableTel()
        If ListeTel Is Nothing Then
            ListeTel = New List(Of Telephone)
        End If
        TLPListeTelephone.Controls.Clear()
        TLPListeTelephone.RowStyles.Clear()
        rowCtr = 0
        For Each tel In ListeTel
            Dim row As New RowStyle(SizeType.AutoSize)
            TLPListeTelephone.RowStyles.Add(row)
            Dim txtTel As New TextBox()
            txtTel.Name = "no" + rowCtr.ToString
            txtTel.Text = tel.Telephone
            txtTel.Tag = rowCtr
            txtTel.Dock = DockStyle.Fill
            txtTel.ReadOnly = _lecture
            txtTel.BackColor = IIf(_lecture, Color.FromArgb(245, 245, 245), Color.White)
            txtTel.ForeColor = Color.Black
            txtTel.MaxLength = 50
            Dim txtDesc As New TextBox()
            txtDesc.Name = "de" + rowCtr.ToString
            txtDesc.Text = tel.Detail
            txtDesc.Tag = rowCtr
            txtDesc.Dock = DockStyle.Fill
            txtDesc.ReadOnly = _lecture
            txtDesc.BackColor = IIf(_lecture, Color.FromArgb(245, 245, 245), Color.White)
            txtDesc.ForeColor = Color.Black
            txtDesc.MaxLength = 50
            AddHandler txtTel.TextChanged, AddressOf TbTel_TextChanged
            AddHandler txtDesc.TextChanged, AddressOf TbTel_TextChanged
            TLPListeTelephone.Controls.Add(txtTel, 0, rowCtr)
            TLPListeTelephone.Controls.Add(txtDesc, 1, rowCtr)
            rowCtr += 1
        Next
        If ListeTel.Where(Function(x) x.Telephone = String.Empty And x.Detail = String.Empty).Count = 0 Then
            If Not _lecture Then
                AddNewTel()
            End If
        Else
            If _lecture Then
                ListeTel.RemoveAt(ListeTel.Count - 1)
                UpdateTableTel()
            End If
        End If
    End Sub

    Private Sub AddNewTel()
        Dim tel As New Telephone()
        tel.Telephone = String.Empty
        tel.Detail = String.Empty
        ListeTel.Add(tel)
        Dim row As New RowStyle(SizeType.AutoSize)
        TLPListeTelephone.RowStyles.Add(row)
        Dim txtTel As New TextBox()
        txtTel.Name = "no" + rowCtr.ToString
        txtTel.Text = tel.Telephone
        txtTel.Tag = rowCtr
        txtTel.Dock = DockStyle.Fill
        Dim txtDesc As New TextBox()
        txtDesc.Name = "de" + rowCtr.ToString
        txtDesc.Text = tel.Detail
        txtDesc.Tag = rowCtr
        txtDesc.Dock = DockStyle.Fill
        AddHandler txtTel.TextChanged, AddressOf TbTel_TextChanged
        AddHandler txtDesc.TextChanged, AddressOf TbTel_TextChanged
        TLPListeTelephone.Controls.Add(txtTel, 0, rowCtr)
        TLPListeTelephone.Controls.Add(txtDesc, 1, rowCtr)
        rowCtr += 1
    End Sub

    Private Sub TbTel_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim indexTel As Integer = Convert.ToInt32(CType(sender, TextBox).Tag)
        If ListeTel.Count = indexTel + 1 Then 'Le dernier téléphone vide
            AddNewTel()
        Else
            If CType(TLPListeTelephone.Controls("no" + indexTel.ToString), TextBox).Text = String.Empty And _
                CType(TLPListeTelephone.Controls("de" + indexTel.ToString), TextBox).Text = String.Empty Then
                ListeTel.RemoveAt(indexTel)
                UpdateTableTel()
                If ListeTel.Count >= indexTel + 1 Then
                    Select Case CType(sender, TextBox).Name.Substring(0, 2)
                        Case "no"
                            Dim txtBox As TextBox = CType(TLPListeTelephone.Controls("no" + indexTel.ToString), TextBox)
                            txtBox.Select(txtBox.Text.Length, 0)
                            txtBox.Focus()
                        Case "de"
                            Dim txtBox As TextBox = CType(TLPListeTelephone.Controls("de" + indexTel.ToString), TextBox)
                            txtBox.Select(txtBox.Text.Length, 0)
                            txtBox.Focus()
                    End Select
                End If
                Return 'Pour ne pas modfier le texte d'un autre
            End If
        End If

        Select Case CType(sender, TextBox).Name.Substring(0, 2)
            Case "no"
                ListeTel(indexTel).Telephone = CType(sender, TextBox).Text
            Case "de"
                ListeTel(indexTel).Detail = CType(sender, TextBox).Text
        End Select
        RaiseEvent modification(True, Nothing)
    End Sub

    Private Sub TbTel_Validating(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim indexTel As Integer = Convert.ToInt32(CType(sender, TextBox).Tag)

    End Sub

    Private Sub UcTelephone_Resize(sender As System.Object, e As System.EventArgs) Handles MyBase.Resize
        TLPListeTelephone.MinimumSize = New Size(TlpEntete.Width, 0)
    End Sub

    Public Function validerTelephone()
        For Each i In ListeTel
            If i.Telephone = String.Empty OrElse i.Detail = String.Empty Then
                Return False
            End If
        Next
        Return True
    End Function

End Class

